package com.example.votemanage.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.votemanage.entity.VoteItem;
import com.example.votemanage.entity.VoteSubject;
import com.example.votemanage.service.IVoteSubjectService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liufangzhou
 * @since 2024-06-06
 */
@Controller
@RequestMapping("/vote-subject")
public class VoteSubjectController {
    @Resource
    IVoteSubjectService voteSubjectService;
    @RequestMapping("/toadd")
    public String toadd(){
        return "add";
    }

    @RequestMapping("/index")
    @ResponseBody
    @CrossOrigin
    public Map findSubjectByPage(
            @RequestParam(name = "pageindex",required = false,defaultValue = "1")int pageindex,
            @RequestParam(name = "pagesize",required = false,defaultValue = "4")int pagesize,
            HttpServletRequest request
    ){
        Page<VoteSubject> page=new Page<>(pageindex,pagesize);
        IPage<VoteSubject> pages=voteSubjectService.findAll(page);
        Map map=new HashMap();
        map.put("subjectPages",pages);
        map.put("pagesize",pagesize);
        for (VoteSubject sub:pages.getRecords()) {
            System.out.println(sub.getVsTitle());
            for(VoteItem item:sub.getItems()){
                System.out.println(item);
            }
        }
        return map;

    }

    @RequestMapping("/findSubjectByVsId")
    public String findSubjectByVsId(int vsid,HttpServletRequest request){
        VoteSubject voteSubject=voteSubjectService.findSubjectByVsId(vsid);
        request.setAttribute("sub",voteSubject);
        return "view";

    }

}
